﻿// -----------------------------------------------------------------------
// <copyright file="Common.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Services
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Entities;
    using System.Data.Objects;

    public class Common
    {
        public static IEnumerable<AuditedOperation> FilterOperations(FilterOperations filter, ref IQueryable<AuditedOperation> operations)
        {
            if (filter.StartDate.HasValue)
            {
                operations = operations.Where(o => o.ReceivedTime.HasValue && EntityFunctions.TruncateTime(o.ReceivedTime) >= EntityFunctions.TruncateTime(filter.StartDate.Value));
            }
            if (filter.EndDate.HasValue)
            {
                operations = operations.Where(o => o.ReceivedTime.HasValue && EntityFunctions.TruncateTime(o.ReceivedTime) <= EntityFunctions.TruncateTime(filter.EndDate.Value));
            }
            if (!string.IsNullOrEmpty(filter.FileType))
            {
                var fileTypes = filter.FileType.Split(new char[1] { ',' });
                operations = operations.Where(o => fileTypes.Contains(o.FileType));
            }
            if (!string.IsNullOrEmpty(filter.FileName))
            {
                operations = operations.Where(o => o.ObjectName.Contains(filter.FileName));
            }
            if (!string.IsNullOrEmpty(filter.User))
            {
                operations = operations.Where(o => o.UserAccount.Contains(filter.User));
            }
            if (!string.IsNullOrEmpty(filter.Host))
            {
                operations = operations.Where(o => o.ServerName.Contains(filter.Host));
            }

            return operations.AsEnumerable<AuditedOperation>();
        }
    
    }
}
